package com.nantai.jiafang.nantaijiafang.activity.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.ProductInfo;

import java.util.List;

public class ProductSpinnerAdapter extends ArrayAdapter<ProductInfo> {

    private Context mContext;

    private List<ProductInfo> list;

    private int resource;

    public ProductSpinnerAdapter(@NonNull Context context, int resource, @NonNull List<ProductInfo> objects) {
        super(context,resource,objects);
        this.resource = resource;
        mContext = context;
        list = objects;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Nullable
    @Override
    public ProductInfo getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        ProductInfo productInfo = list.get(position);

        if(convertView == null){
            viewHolder = new ViewHolder();

            convertView = LayoutInflater.from(mContext).inflate(resource, null);

            viewHolder.productName = (TextView) convertView.findViewById(R.id.product_name);

//            viewHolder.productValue = (TextView) convertView.findViewById(R.id.product_value);

            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.productName.setText(productInfo.getcInvName());

//        viewHolder.productValue.setText(productInfo.getcInvCode());

        return convertView;
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder;

        ProductInfo productInfo = list.get(position);

        if(convertView == null){
            viewHolder = new ViewHolder();

            convertView = LayoutInflater.from(mContext).inflate(resource, null);

            viewHolder.productName = (TextView) convertView.findViewById(R.id.product_name);

//            viewHolder.productValue = (TextView) convertView.findViewById(R.id.product_value);

            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.productName.setText(productInfo.getcInvName());

//        viewHolder.productValue.setText(productInfo.getcInvCode());

        return convertView;
    }

    static class ViewHolder{
        TextView productName,productValue;
    }
}
